﻿
    using Chain.BLL;
    using System;
    using System.Data;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public partial class giftorders : Page
    {

        private int TotalSubtotal;

        private void DataBindGiftCard(HttpCookie cookie)
        {
            PointGift PointGiftBll = new PointGift();
            StringBuilder sbWhere = new StringBuilder("GiftID in(");
            string[] keys = HttpUtility.UrlDecode(cookie.Value).Trim().Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string item in keys)
            {
                int GiftID = Convert.ToInt32(item.Split(new char[] { ',' })[0]);
                Convert.ToInt32(item.Split(new char[] { ',' })[1]);
                sbWhere.AppendFormat("{0},", GiftID);
            }
            sbWhere.Append("0)");
            DataTable dt = PointGiftBll.GetList(sbWhere.ToString()).Tables[0];
            dt = this.UpDateTable(dt, keys);
            this.rptGiftsinventory.DataSource = dt;
            this.rptGiftsinventory.DataBind();
        }

        private string GetGiftlist(int ExchangeID)
        {
            StringBuilder sbGiftlist = new StringBuilder();
            DataTable dt = new MicroWebsiteGiftExchangeDetail().GetGiftExchangeDetailByExchangeID(ExchangeID);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sbGiftlist.AppendFormat("{0},{1}|", dt.Rows[i]["ExchangeGiftID"], dt.Rows[i]["ExchangeNumber"]);
            }
            return sbGiftlist.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie cookie = base.Request.Cookies["giftlist"];
            string type = base.Request["type"];
            if (!string.IsNullOrEmpty(type) && (type == "lookinventory"))
            {
                this.giftsordersinfo.Style.Add("display", "none");
                this.aGiftOrderSubmit.Style.Add("display", "none");
                this.spTotalPointDesc.InnerText = "总积分";
                cookie.Value = this.GetGiftlist(Convert.ToInt32(base.Request["giftlist"]));
                this.card.Attributes.Add("class", "ui-btn-active");
            }
            else
            {
                this.convert.Attributes.Add("class", "ui-btn-active");
            }
            if ((cookie != null) && !string.IsNullOrEmpty(cookie.Value))
            {
                this.DataBindGiftCard(cookie);
                this.totalPoint.InnerText = this.TotalSubtotal.ToString();
            }
        }

        private DataTable UpDateTable(DataTable dt, string[] keys)
        {
            dt.Columns.Add("ConvertNumber");
            dt.Columns.Add("Subtotal");
            foreach (string item in keys)
            {
                int GiftID = Convert.ToInt32(item.Split(new char[] { ',' })[0]);
                int ConvertNumber = Convert.ToInt32(item.Split(new char[] { ',' })[1]);
                DataRow row = dt.Select("GiftID=" + GiftID)[0];
                row["ConvertNumber"] = ConvertNumber;
                row["Subtotal"] = ConvertNumber * Convert.ToInt32(row["GiftExchangePoint"]);
                this.TotalSubtotal += Convert.ToInt32(row["Subtotal"]);
            }
            return dt;
        }
    }


